#!/usr/bin/env
#coding:utf-8
#自定义log类库
import logging
import sys
from datetime import datetime, timezone, timedelta
sys.stdout.flush()
dt = datetime.utcnow()
dt = dt.replace(tzinfo = timezone.utc)
tzutc_8 = timezone(timedelta(hours = 8))
datefmt = datetime.now(tzutc_8).strftime("%Y-%m-%d %H:%M:%S")
format_dict = {
    1: logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"),
    2: logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"),
    3: logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"),
    4: logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"),
    5: logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
}
class Logger:
    def __init__(self, logname, loglevel, event):
        '''
        指定保存日志的文件路径，日志级别，以及调用文件
        使用方法实例
        定义Logs日志存储路径
        logsFile = "*.logs"
        定义输出信息
        message = "[当前订单号:201711092106]"
        调用类实例
        logger = Logger(logsFile, loglevel=1, logger=message).debug(message)
        将日志存入到指定的文件中
        创建一个logger
        创建一个handler，用于写入日志文件
        打印到控制台
        格式化输出格式
        '''
        # 创建一个logger

        self.logger = logging.getLogger(event)
        self.logger.setLevel(logging.DEBUG)
        if not self.logger.handlers:
            # 创建一个handler，用于写入日志文件
            fh = logging.FileHandler(logname)
            fh.setLevel(logging.DEBUG)
            # 打印到控制台
            ch = logging.StreamHandler()
            ch.setLevel(logging.DEBUG)
            # 格式化输出格式
            formatter = format_dict[int(loglevel)]
            fh.setFormatter(formatter)
            ch.setFormatter(formatter)
            self.logger.addHandler(ch)
            self.logger.addHandler(fh)
            sys.stdout.flush()

    def getlog(self):
        '''
        返回实例
        :return: instance
        '''
        return self.logger

    def info(self,message):
        '''
        :param message: 输出信息
        :return: 返回 级别信息输出
        '''
        sys.stdout.flush()
        self.logger.info(message)
        self.closeHandle()


    def error(self, message):
        '''
        返回错误级别信息
        :param message: 输出信息
        :return:str
        '''
        sys.stdout.flush()
        self.logger.error(message)
        self.closeHandle()

    def warning(self, message):
        '''
        返回警告级别信息
        :param message: 输出信息
        :return:str
        '''
        sys.stdout.flush()
        self.logger.warning(message)
        self.closeHandle()

    def critical(self, message):
        '''
        返回致命错误信息
        :param message: 输出致命信息
        :return: str
        '''
        sys.stdout.flush()
        self.logger.critical(message)
        self.closeHandle()

    def debug(self, message):
        '''
        返回debug信息
        :param message:输出debug信息
        :return:
        '''
        sys.stdout.flush()
        self.logger.debug(message)
        self.closeHandle()

    def closeHandle(self):
        self.logger.handler = []
        return self.logger.handler

